package logic

import (
	"context"
	"fmt"
	"rpc/model/mysql"

	"rpc/internal/svc"
	"rpc/turn"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserListLogic {
	return &UserListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserListLogic) UserList(in *turn.UserListRequest) (*turn.UserListResponse, error) {
	// todo: add your logic here and delete this line
	u := mysql.User{}

	list, err := u.UserList(in.Status)
	if err != nil {
		logx.Error("用户列表展示失败")
		return nil, fmt.Errorf("用户列表展示失败")
	}
	var List []*turn.UserItem
	for _, user := range list {
		List = append(List, &turn.UserItem{
			Tel:      user.Tel,
			Password: user.Password,
			Status:   int64(user.Status),
		})
	}

	return &turn.UserListResponse{UserItem: List}, nil
}
